﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace atirado
{
    class Player
    {
        SpriteBatch spriteBatch;

         public Vector2 posicaoplayer;
        float velocidadey = 0f;
        Texture2D textura4;
        List<Tiro> listTiro;
        Texture2D miraTank;
        float rotation;
        Texture2D mouse;
        Vector2 posims;
        int tempo = 0;
        public int vidas = 3;
        public int matanca = 0;
        float vmax = 2.5f;
        int atraso = 200;
        float angulotank = MathHelper.Pi;
        bool morto = false;
        Inimigo inimigo;



        public Player(Texture2D texTiro, Texture2D tiro22,Texture2D miraTank, Texture2D textura4, Texture2D mouse, Vector2 posicao, SpriteBatch spriteBatch)
        {
            this.spriteBatch = spriteBatch;
            this.posicaoplayer = posicao;
            this.mouse = mouse;
            this.textura4 = textura4;
            this.miraTank = miraTank;
            this.vidas = 3;
            this.matanca = 0;
            posicao = Vector2.Zero;
            Tiro.setImagem(texTiro);
            tiro2.setImagem(texTiro);
            
            listTiro = new List<Tiro>();
        }

        public List<Tiro> Update(KeyboardState state, MouseState MS, GameTime GT,int matanca)
        {
            bool esquerda = state.IsKeyDown(Keys.A);
            bool direita = state.IsKeyDown(Keys.D);
            bool frente = state.IsKeyDown(Keys.W);
            bool traz = state.IsKeyDown(Keys.S);

            
            if (!morto)
            {

                foreach (Tiro t in listTiro)
                {
                    t.update();
                }
                if (esquerda)
                {
                    angulotank -= MathHelper.Pi / 180;
                }
                if (direita)
                {
                    angulotank += MathHelper.Pi / 180;
                }

                if (frente)
                {
                    if (velocidadey <= -vmax)
                    { }
                    else
                        velocidadey -= 0.05f;
                }
                if (traz)
                {
                    if (velocidadey >= vmax)
                    { }
                    else
                        velocidadey += 0.05f;
                }
                if (matanca > 15) 
                {
                    atraso = 100;
                }
            }

            if (!(esquerda || direita || frente || traz))
            {


                if (velocidadey > 0)
                {
                    velocidadey -= 0.09f;
                }
                if (velocidadey < 0)
                {
                    velocidadey += 0.09f;
                }
            }

            posims = new Vector2(MS.X, MS.Y);
            float distx = posims.X - posicaoplayer.X;
            float disty = posims.Y - posicaoplayer.Y;
            rotation = (float)Math.Atan2(disty, distx);

            tempo += GT.ElapsedGameTime.Milliseconds;

            if (MS.LeftButton == ButtonState.Pressed)
            {
                if (tempo >= atraso)
                {
                    listTiro.Add(new Tiro(posicaoplayer, rotation));
                    tempo = 0;
                }
            }
            if (posicaoplayer.X > 750)
            {
                posicaoplayer.X = 750;
            }

            if (posicaoplayer.X < 45) 
            {
                posicaoplayer.X = 45;
            }

            if (posicaoplayer.Y < 45) 
            {
                posicaoplayer.Y = 45;
            }

            if (posicaoplayer.Y > 450)
            {
                posicaoplayer.Y = 450;
            }

            //if (vidas  == 0) 
            //{
            //    morto = true;
            //}

            
            posicaoplayer.X += velocidadey * (float)Math.Sin(angulotank);
            posicaoplayer.Y -= velocidadey * (float)Math.Cos(angulotank);

            return listTiro;
        }
        public bool estamorto() 
        {
            return morto;
        }
        public Rectangle colisao()
        {
            return new Rectangle((int)posicaoplayer.X, (int)posicaoplayer.Y, textura4.Bounds.Width, textura4.Bounds.Height);
        }
        public void sangue()
        {
            morto = true;
        }
        
        
        public void Draw(GameTime gameTime)
        {

            foreach (Tiro t in listTiro)
            {
                t.Draw(spriteBatch);
            }
            if (!morto)
            {
                

                if (vidas < 3)
                {
                    spriteBatch.Draw(textura4, new Vector2(posicaoplayer.X, posicaoplayer.Y), null, Color.Red, rotation + MathHelper.PiOver2, new Vector2(9, 42), 1f, SpriteEffects.None, 1f);
                    spriteBatch.Draw(miraTank, new Vector2(posicaoplayer.X, posicaoplayer.Y), null, Color.Red, angulotank + MathHelper.PiOver2, new Vector2(31, 52), 1, SpriteEffects.None, 0.9f);
                    spriteBatch.Draw(mouse, posims, null, Color.White, 0f, new Vector2(mouse.Bounds.Center.X, mouse.Bounds.Center.Y), 1, SpriteEffects.None, 0.6f);
                }
                else
                {
                    spriteBatch.Draw(textura4, new Vector2(posicaoplayer.X, posicaoplayer.Y), null, Color.White, rotation + MathHelper.PiOver2, new Vector2(9, 42), 1f, SpriteEffects.None, 1f);
                    spriteBatch.Draw(miraTank, new Vector2(posicaoplayer.X, posicaoplayer.Y), null, Color.White, angulotank + MathHelper.PiOver2, new Vector2(31, 52), 1, SpriteEffects.None, 0.9f);
                    spriteBatch.Draw(mouse, posims, null, Color.White, 0f, new Vector2(mouse.Bounds.Center.X, mouse.Bounds.Center.Y), 1, SpriteEffects.None, 0.6f);
                }

            }
            
        }
    }
}
